<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: activeIndex === index }"
        @click="menuChange(index)"
      >
        <div class="role-info">
          <svg-icon
            :icon-class="activeIndex === index ? 'user-active' : 'user'"
            class="icon"
          />
          {{ item.roleName }}
        </div>
        <!-- 更多 -->
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editRole(item.roleId)"
                >编辑角色</el-dropdown-item
              >
              <el-dropdown-item @click.native="delRole(item.roleId)"
                >删除</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')"
        >添加角色</el-button
      >
    </div>

    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                node-key="id"
                :data="item.children"
                :props="defaultProps"
                default-expand-all
                show-checkbox
              />
            </div>
          </div>
        </el-tab-pane>
        <!-- 模版渲染 -->
        <el-tab-pane :label="`成员(${total})`" name="employee">
          <div class="user-wrapper">
            <el-table :data="userList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
  getAllRoleAPI,
  getAllRoleTreeAPI,
  getRoleDetailAPI,
  getUserListAPI,
  delRoleUserAPI
} from '@/api/system'
export default {
  name: 'Role',
  data () {
    return {
      roleList: [], // 角色列表
      activeIndex: 0, // 当前菜单索引
      treeList: [], // 权限树形列表
      // 配置选项
      defaultProps: {
        label: 'title',
        disabled: () => true
      },
      activeName: 'tree',
      // 请求参数
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0, // 总条数
      userList: [] // 用户列表
    }
  },
  async created () {
    await this.getAllRole()
    await this.getAllRoleTree()

    this.menuChange(0)
  },
  methods: {
    // 删除
    delRole (id) {
      this.$confirm('你确定要删除吗?', '温馨提示')
        .then(async () => {
          await delRoleUserAPI(id)
          this.$message.success('删除成功')
          this.getAllRole()
        })
        .catch(() => {})
    },
    // 编辑
    editRole (id) {
      // console.log(id)
      this.$router.push(`/roleAdd?id=${id}`)
    },
    // 获取角色关联列表
    async getUserList (roleId) {
      const res = await getUserListAPI(roleId, this.params)
      this.total = res.data.total
      this.userList = res.data.rows
      // console.log('获取角色关联列表',this.userList)
    },
    // 查询所有功能权限(树形)
    async getAllRoleTree () {
      const res = await getAllRoleTreeAPI()
      this.treeList = res.data
    },
    // 菜单切换事件
    menuChange (index) {
      this.activeIndex = index // 更新当前菜单索引
      const roleId = this.roleList[index].roleId
      this.getRoleDetail(roleId)
      this.getUserList(roleId) // 获取角色关联列表
    },
    // 获取当前角色权限点列表
    async getRoleDetail (id) {
      const res = await getRoleDetailAPI(id)
      const perms = res.data.perms
      // console.log('用户的权限', perms)
      const treeComponent = this.$refs.tree
      treeComponent.forEach((tree, index) => {
        tree.setCheckedKeys(perms[index])
      })
    },
    // 查询所有角色
    async getAllRole () {
      const res = await getAllRoleAPI()
      // console.log(res)
      this.roleList = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
